package com.wzq.permission.client.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.wzq.permission.client.model.EasyUiTree;
import com.wzq.permission.client.model.admin.MenuModel;
import com.wzq.permission.client.model.admin.OrganizeModel;
import com.wzq.permission.client.model.admin.OrganizeRoleModel;

public class TreeUtil {
	/**
	 * menuModel列表转成easyUi tree
	 * @param menuModelList
	 * @return List<EasyUiTree>
	 */
	public static List<EasyUiTree> menuModelListToEasyUiTree(List<MenuModel> menuModelList){
		List<EasyUiTree> treeList = new ArrayList<EasyUiTree>();
		if(menuModelList != null){
			for(MenuModel menuModel:menuModelList){
				EasyUiTree tree = new EasyUiTree<EasyUiTree>();
				tree.setId(menuModel.getMenuId());
				tree.setText(menuModel.getMenuName());
				if(menuModel.getChildrens()!=null){
					List<EasyUiTree> tempTree = menuModelListToEasyUiTree(menuModel.getChildrens());
					tree.setChildren(tempTree);
				}
				treeList.add(tree);
			}
		}
		return treeList;
	}
	
	/**
	 * menuList to 树形结构
	 * @param menuList 菜单列表
	 * @return List<MenuModel>
	 */
	public static List<MenuModel> menuListToTree(List<MenuModel> menuList){
		List<MenuModel> result = new ArrayList<MenuModel>();
		if(menuList!=null){
			for(MenuModel menuModel:menuList){
				if(menuModel.getParentId()==-1){
					result.add(menuModel);
				}
				for(MenuModel menuModel2:menuList){
					if(menuModel2.getParentId().longValue()==menuModel.getMenuId().longValue()){
						if(menuModel.getChildrens()==null){
							List<MenuModel> childrens = new ArrayList<MenuModel>();
							childrens.add(menuModel2);
							menuModel.setChildrens(childrens);
						}else{
							menuModel.getChildrens().add(menuModel2);
						}
					}
				}
			}
		}
		return result;
	}

	/**
	 * organizeModel列表转成easyUi tree
	 * 
	 * @param orgModelList
	 * @return
	 */
	public static List<EasyUiTree> orgModelListToEasyUiTree(
			List<OrganizeModel> orgModelList) {
		List<EasyUiTree> treeList = new ArrayList<EasyUiTree>();
		if (orgModelList != null) {
			for (OrganizeModel orgModel : orgModelList) {
				EasyUiTree tree = new EasyUiTree<EasyUiTree>();
				tree.setId(orgModel.getOrganizeId());
				tree.setText(orgModel.getOrganizeName());
				if (orgModel.getChildrens() != null) {
					List<EasyUiTree> tempTree = orgModelListToEasyUiTree(orgModel
							.getChildrens());
					tree.setChildren(tempTree);
				}
				treeList.add(tree);
			}
		}
		return treeList;
	}

	/**
	 * menuList to 树形结构
	 * 
	 * @param menuList
	 *            菜单列表
	 * @return List<MenuModel>
	 */
	public static List<OrganizeModel> orgListToTree(List<OrganizeModel> orgList) {
		List<OrganizeModel> result = new ArrayList<OrganizeModel>();
		if (orgList != null) {
			for (OrganizeModel orgModel : orgList) {
				if (orgModel.getParentId() == 1) {
					result.add(orgModel);
				}
				for (OrganizeModel orgModel2 : orgList) {
					if (orgModel2.getParentId() == orgModel.getOrganizeId()) {
						if (orgModel.getChildrens() == null) {
							List<OrganizeModel> childrens = new ArrayList<OrganizeModel>();
							childrens.add(orgModel2);
							orgModel.setChildrens(childrens);
						} else {
							orgModel.getChildrens().add(orgModel2);
						}
					}
				}
			}
		}
		return result;
	}
	
	/**
	 * 角色树
	 * @param orgRoleList
	 * @return
	 */
	public static List<OrganizeRoleModel> roleListToTree(List<OrganizeRoleModel> orgRoleList) {
		List<OrganizeRoleModel> result = new ArrayList<OrganizeRoleModel>();
		if (orgRoleList != null) {
			Map<Long, String> appIdMap = new HashMap<Long, String>();
			for (OrganizeRoleModel orgModel : orgRoleList) {
				appIdMap.put(orgModel.getApplicationId(), orgModel.getApplicationName());
			}
			Iterator it = appIdMap.entrySet().iterator();
			while(it.hasNext()){
				Map.Entry entry = (Map.Entry) it.next();
				Long key = (Long) entry.getKey();
				String val = (String) entry.getValue();
				OrganizeRoleModel  orgRole = new OrganizeRoleModel();
				orgRole.setApplicationId(key);
				orgRole.setApplicationName(val);
				orgRole.setRoleName(val);
				result.add(orgRole);
				
				for (OrganizeRoleModel orgModel2 : orgRoleList) {
					if (orgRole.getApplicationId() == orgModel2.getApplicationId()) {
						if (orgRole.getChildrens() == null) {
							List<OrganizeRoleModel> childrens = new ArrayList<OrganizeRoleModel>();
							childrens.add(orgModel2);
							orgRole.setChildrens(childrens);
						} else {
							orgRole.getChildrens().add(orgModel2);
						}
					}
				}
			}
			
		}
		return result;
	}
	
	
	/**
	 * OrganizeRoleModel列表转成easyUi tree
	 * 
	 * @param orgRolelList
	 * @return
	 */
	public static List<EasyUiTree> orgRoleListToEasyUiTree(List<OrganizeRoleModel> orgRolelList) {
		List<EasyUiTree> treeList = new ArrayList<EasyUiTree>();
		if (orgRolelList != null) {
			for (OrganizeRoleModel orgModel : orgRolelList) {
				EasyUiTree tree = new EasyUiTree<EasyUiTree>();
				tree.setId(orgModel.getRoleId());
				tree.setText(orgModel.getRoleName());
				if (orgModel.getChildrens() != null) {
					List<EasyUiTree> tempTree = orgRoleListToEasyUiTree(orgModel
							.getChildrens());
					tree.setChildren(tempTree);
				}
				treeList.add(tree);
			}
		}
		return treeList;
	}
	
}
